import re
from django import forms
from django.utils.translation import gettext_lazy as _

from fontrender.settings import MIN_FONT_SIZE,MAX_FONT_SIZE,MIN_LEADING,MAX_LEADING

TRACKING_CHOICE = (
    (-1,'-100%'),
    (-0.75,'-75%'),
    (-0.5,'-50%'),
    (-0.25,'-25%'),
    (-0.1,'-10%'),
    (-0.05,'-5%'),    
    (0,'0'),
    (0.05,'+5%'),
    (0.1,'+10%'),    
    (0.25,'+25%'),
    (0.5,'+50%'),
    (0.75,'+75%'),
    (1,'+100%'),
)

ALIGNMENT_CHOICE = (
    ('left',_('Flush left')),
    ('right',_('Flush right')),                    
    ('justified',_('Justified')),
    ('center',_('Centered')),
)

class ConverterForm(forms.Form):
    size_choices = range(MIN_FONT_SIZE,MAX_FONT_SIZE+1)
    leading_choices = ['auto']+range(MIN_LEADING,MAX_LEADING+1)

    size = forms.ChoiceField(choices=zip(size_choices,size_choices),required=True)
    color = forms.CharField()
    alignment = forms.ChoiceField(choices=ALIGNMENT_CHOICE,required=True)    
    letter_spacing = forms.ChoiceField(choices=TRACKING_CHOICE,required=True)
    leading = forms.ChoiceField(choices=zip(leading_choices,leading_choices),required=True)
    vertical_scale = forms.IntegerField(min_value=0,max_value=1000,required=True)
    horizontal_scale = forms.IntegerField(min_value=0,max_value=1000,required=True)
    bold = forms.BooleanField(required=False)
    italic = forms.BooleanField(required=False)
    underline = forms.BooleanField(required=False)
    message = forms.CharField(widget=forms.Textarea,required=True)

    def clean_color(self):
        color = self.cleaned_data['color'].strip()
        w = re.match(r'^([a-f0-9]{6})$', color)
        if w==None:
            raise forms.ValidationError(_("Input color %(col)s is not in #RRGGBB format") % {'col': color})
        return color
